package cn.yunyichina.provider.hquery.thread;

import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.exception.CallException;
import cn.yunyichina.provider.hquery.dao.CheckBodyDao;
import cn.yunyichina.provider.hquery.entity.CheckBody;
import cn.yunyichina.provider.hquery.vo.ExaminationVo;
import cn.yunyichina.provider.iface.entity.base.MedicalCardVo;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineListVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 15-AK on 2016/12/15.
 */
public class ExamineListRunnable  implements Runnable{
	
    private static final Logger logger = LoggerFactory.getLogger(ExamineListRunnable.class);
    
    private List<ExamineListVoResponse> examineListVoResponseList;
    
    private ExaminationVo requestVo;
    
    private CheckBodyDao checkBodyDao = SpringContextHandler.getBean(CheckBodyDao.class);
    
    //private CheckBodyDetailDao checkBodyDetailDao = SpringContextHandler.getBean(CheckBodyDetailDao.class);
    
    public ExamineListRunnable(List<ExamineListVoResponse> examineListVoResponseList,ExaminationVo requestVo) {
        this.examineListVoResponseList = examineListVoResponseList;
        this.requestVo = requestVo;
    }

    @Override
    public void run() {

        logger.info("保存检查列表记录开始======examineListVoResponseList ="+JsonUtils.toJsonString(examineListVoResponseList));
        
        //查询绑定卡
        logger.info("【检查列表保存线程处理-查询绑定卡信息】, yyt.base.medicalcard.by.id.get request=" + JSON.toJSONString(requestVo));
        MedicalCardVo medicalCard =  BaseService.getMedicalCardByCardNo(requestVo.getHospitalCode(),requestVo.getPatCardNo(),requestVo.getPatCardType(),requestVo.getAppId(),null);
        if (medicalCard == null) {
            throw new CallException("18010","找不到绑定卡信息 card_no=" +requestVo.getPatCardNo());
        }
        logger.info("【检查列表保存线程处理-查询绑定卡信息】, yyt.base.medicalcard.by.id.get response=" + JSONObject.toJSONString(medicalCard));
        // 查询组织信息-----------------------------------------------start
        Map<String, String> orgParamMap = new HashMap<String, String>();
        orgParamMap.put("org_code", requestVo.getHospitalCode());
        logger.info("【检查列表-查询组织信息】, yyt.base.branchhospital.org.list request=" + JSON.toJSONString(orgParamMap));
        Response orgResponse = BaseService.callUnderlineFromInternal("yyt.base.branchhospital.org.list", JSON.toJSONString(orgParamMap));
        logger.info("【检查列表-查询组织信息】, yyt.base.branchhospital.org.list response=" + JSONObject.toJSONString(orgResponse));
        if (!"0".equals(orgResponse.getResultCode())) {
            throw new CallException("18010","找不到机构信息 by org_code=" + requestVo.getHospitalCode());
        }
        List<Org> orgList = JsonUtils.parseArray(orgResponse.getResult(), Org.class);
        if (org.springframework.util.CollectionUtils.isEmpty(orgList)) {
            throw new CallException("18010","找不到机构信息 by org_code=" + requestVo.getHospitalCode());
        }
        Org org = orgList.get(0);
        //存检查基本信息
        if (examineListVoResponseList != null && examineListVoResponseList.size() > 0) {
        	Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("orgCode", requestVo.getHospitalCode());
			//paramMap.put("branchHospitalCode", reportVo.getBranchHospitalCode());
			List<String> checkIds = new ArrayList<String>();
			for (ExamineListVoResponse examine: examineListVoResponseList) {
				checkIds.add(examine.getCheckId());
			}
			paramMap.put("checkIds", checkIds);
			List<String> existsCheckIds = checkBodyDao.findByOrgCodeAndcheckIds(paramMap);
			
			//存检查基本信息
			CheckBody checkBody = null;
	        for (ExamineListVoResponse vo : examineListVoResponseList) {
	        	//数据库不存在检查记录就将记录存进数据库
	        	if (!existsCheckIds.contains(vo.getCheckId())) {
	        		checkBody = new CheckBody();
	        		checkBody.setId(PKGenerator.generateId());
	        		checkBody.setOrgName(org.getOrgName());
	        		checkBody.setOrgCode(requestVo.getHospitalCode());
	        		checkBody.setOrgId(org.getId());
	        		checkBody.setBranchCode(requestVo.getBranchCode());
	        		checkBody.setBranchId("");
	        		checkBody.setBranchName("");
	        		checkBody.setPatId(medicalCard.getPatId());
	        		checkBody.setPatName(medicalCard.getPatName());
	        		checkBody.setPatSex(medicalCard.getPatSex());
	        		checkBody.setPatAge(medicalCard.getPatAge());
	        		checkBody.setPatMobile(medicalCard.getPatMobile());
	        		checkBody.setPatAddress(medicalCard.getPatAddress());
	        		checkBody.setIdCardType(medicalCard.getIdCardType());
	        		checkBody.setIdCardNo(medicalCard.getIdCardNo());
	        		checkBody.setMedicalCardNo(medicalCard.getMedicalCardNo());
	        		checkBody.setMedicalCardType(medicalCard.getMedicalCardType());
	        		checkBody.setAdmissionNo(medicalCard.getAdmissionNo());
	        		checkBody.setCheckType("".equals(vo.getCheckType())?null:Short.valueOf(vo.getCheckType()));
	        		checkBody.setCheckId(vo.getCheckId());
	        		checkBody.setCheckName(vo.getCheckName());
	        		checkBody.setReportTime("".equals(vo.getReportTime())?null:vo.getReportTime());
	        		checkBody.setFileAddress(vo.getFileAddress());
	        		checkBody.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	        		
	        		//插入检查列表信息数据表
	        		logger.info("要新增的检查列表=="+JsonUtils.toJsonString(checkBody));
	        		checkBodyDao.insert(checkBody); 
	        	}
	        }
        }
    }
}
